package router

import (
	"fmt"
	v1 "gin_server/api/v1"
	"gin_server/middleware"

	"github.com/spf13/viper"

	"github.com/gin-gonic/gin"
)

// InitRouter 路由初始化
func InitRouter() {
	mode := viper.GetString("app.mode")
	gin.SetMode(mode)
	r := gin.Default()
	r.Use(middleware.Cors())
	// 生产环境产生自定义日志
	if mode != "debug" {
		r = gin.New()
		r.Use(middleware.Logger(), gin.Recovery())
	}

	router := r.Group("api/v1")
	{
		router.POST("/login", v1.Login)
		router.GET("/user", v1.GetUser)
		router.POST("/user", v1.UserCreate)
	}
	// 需要鉴权的路由
	auth := r.Group("api/v1")
	auth.Use(middleware.JwtToken())
	{
		// auth.GET("/user", v1.GetUser)
		// router.POST("/user", v1.UserCreate)
		//router.GET("/user", v1.GetUser)
		//router.DELETE("/user/:id", v1.DeleteUser)
		//router.PUT("/user", v1.UpdateUser)
	}
	port := viper.GetString("server.port")
	r.Run(fmt.Sprintf(":%s", port))
}
